package treeedit;

import java.awt.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.net.*;

public class Configuration 
{
	Properties conf = new Properties();
	static private Configuration instance = null;
	Map<String,Icon> iconMap = new HashMap<String,Icon>();
	
	public static Configuration getInstance()
	{
		if(instance == null)
			instance = new Configuration();
		return instance;
	}

	private Configuration() {
		try {
			InputStream input = Configuration.class.getResourceAsStream("default.conf");
//			Properties deft = new Properties();
			conf.load(input);
		} catch (Exception ex) {
			System.out.println("Default config not found");
		}
		try {
			String userHome = System.getProperty("user.home");
			
			userHome = ( userHome.endsWith(System.getProperty("file.separator"))?userHome:userHome+System.getProperty("file.separator"));
			
//			System.out.println("USER HOME:" + userHome + "TreeEdit.conf");
			conf.load(new FileInputStream(userHome+"TreeEdit.conf"));
		} catch (Exception ex) {
			System.out.println("User config not found");
		}

	}
	
	public void store() {
		try {
			String userHome = System.getProperty("user.home");
			userHome = ( userHome.endsWith(System.getProperty("file.separator"))?userHome:userHome+System.getProperty("file.separator"));
//			System.out.println("Storing:" + userHome+ "TreeEdit.conf");
			conf.store(new FileOutputStream(userHome+"TreeEdit.conf"),"no comment");
		} catch (IOException ex) {
			System.out.println("Couldn't save settings");
		}
	}
	
	public String getClispPath() {
		return conf.getProperty("clisp");
	}
	public void setLispPath(String newPath) {
//		System.out.println("Changing path to " + newPath);
		conf.setProperty("clisp",newPath);
	}
	
	public Dimension getDimensions() {
		String [] dims = conf.getProperty("dimension", "600,600").split(",");
		int width = new Integer(dims[0]);
		int height = new Integer(dims[1]);
		return new Dimension(width, height);
	}

	public void setDimensions(Dimension dim) {
		conf.setProperty("dimension",dim.width + "," + dim.height);
	}

	public Point getLocation() {
		String [] dims = conf.getProperty("location", "60,60").split(",");
		int x = new Integer(dims[0]);
		int y = new Integer(dims[1]);
		return new Point(x,y);
	}

	public void setLocation(Point dim) {
		conf.setProperty("location",dim.x + "," + dim.y);
	}

	public Object getRecentFile(int i) {
		if (i < getRecentFileCount())
			return conf.getProperty("recentFiles","").split(",")[i];
		return null;
	}

	public void addRecentFile(String file) {
		String putprop = "";
		String files = conf.getProperty("recentFiles");
		if(!(files == null) &&  files.contains(file)) return ;
		putprop += file;
		for(int i = 0; i< getRecentFileCount(); i++)
			putprop +=  "," + getRecentFile(i);
		
		conf.setProperty("recentFiles",putprop);
	}

	public int getRecentFileCount() {
		if (conf.getProperty("recentFiles")==null) return 0;
		return conf.getProperty("recentFiles", "").split(",").length;
	}

	public Icon getFunctionIcon() {
		return openIcon("functionIcon");
	}

	public void setFunctionIcon(String filename) {
		setIcon("functionIcon",filename);
	}
	
	public Icon getStringIcon() {
		return openIcon("stringIcon");
	}
	
	public void setStringIcon(String filename) {
		setIcon("stringIcon",filename);
	}
	
	public Icon getQuoteIcon() {
		return openIcon("quoteIcon");
	}
	
	public void setQuoteFunctionIcon(String filename) {
		setIcon("quoteIcon",filename);
	}
	
	public Icon getNormalIcon() {
		return openIcon("normalIcon");
	}

	public void setNormalIcon(String filename) {
		setIcon("normalIcon",filename);
	}
	
	public Icon getMarkedIcon() {
		return openIcon("markedIcon");
	}

	public void setMarkedIcon(String filename) {
		setIcon("markedIcon",filename);
	}

	private void setIcon(String iconName, String filename) {
		iconMap.put(iconName,new ImageIcon(filename));
		conf.setProperty(iconName, filename);
	}
	
	private Icon openIcon(String property) {
		if(iconMap.get(property)==null) {
			String filename = conf.getProperty(property, "");
			Icon ret = null;
			if (filename != null){
				URL url = Configuration.class.getResource(filename);
				try {
					ret =  new ImageIcon(url);
				} catch (Exception ex) {
					System.out.println("Coudn't open file " + url);
				}
			}
			iconMap.put(property,ret);
		}
		return iconMap.get(property);
	}
}
